package cn.csc.module.product.controller.admin.category;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.api.category.dto.CategoryRespDTO;
import cn.csc.module.product.controller.admin.category.vo.*;
import cn.csc.module.product.convert.category.CategoryConvert;
import cn.csc.module.product.dal.dataobject.category.CategoryDO;
import cn.csc.module.product.service.category.CategoryService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Slf4j
@Api(tags = "管理后台 - 分类信息")
@RestController
@RequestMapping("/product/category")
@Validated
public class CategoryController {

    @Resource
    private CategoryService categoryService;
    @Autowired
    ObjectMapper objectMapper;

    @PostMapping("/create")
    @ApiOperation("创建分类信息")
    @PreAuthorize("@ss.hasPermission('product:category:create')")
    public CommonResult<BigInteger> createCategory(@Valid @RequestBody CategoryCreateReqVO createReqVO) {
        return success(categoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新分类信息")
    @PreAuthorize("@ss.hasPermission('product:category:update')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody CategoryUpdateReqVO updateReqVO) {
        categoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除分类信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:category:delete')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") BigInteger id) {
        categoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得分类信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:category:query')")
    public CommonResult<CategoryRespVO> getCategory(@RequestParam("id") BigInteger id) {
        CategoryDO category = categoryService.getCategory(id);
        return success(CategoryConvert.INSTANCE.convert(category));
    }

//    @GetMapping("/list")
//    @ApiOperation("获得分类信息列表")
//    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
//    @PreAuthorize("@ss.hasPermission('product:category:query')")
//    public CommonResult<List<CategoryRespVO>> getCategoryList(@RequestParam("ids") Collection<BigInteger> ids) {
//        List<CategoryDO> list = categoryService.getCategoryList(ids);
//        return success(CategoryConvert.INSTANCE.convertList(list));
//    }

    @GetMapping("/list")
    @ApiOperation("获得分类信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺ID", required = false, example = "1024", dataTypeClass = BigInteger.class),
            @ApiImplicitParam(name = "parentId", value = "父节点", required = false, example = "1024", dataTypeClass = BigInteger.class),
            @ApiImplicitParam(name = "name", value = "类目名称", required = false, example = "鞋子", dataTypeClass = String.class)
    })
    @PreAuthorize("@ss.hasPermission('product:category:query')")
    public CommonResult<List<CategoryRespVO>> getCategoryList(CategoryCreateReqVO createReqVO) throws JsonProcessingException {
        List<CategoryDO> list = categoryService.listByParenId(createReqVO);
        return success(CategoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/listByPid")
    @ApiOperation("获得分类集合信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:category:query')")
    public CommonResult<List<CategoryRespDTO>> getCategoryListByPid(@RequestParam("shopId") BigInteger shopId, @RequestParam("parentId") BigInteger parentId) {
        List<CategoryRespDTO> category = categoryService.listByShopIdAndParenId(shopId, parentId);
        return success(category);
    }

    @GetMapping("/page")
    @ApiOperation("获得分类信息分页")
    @PreAuthorize("@ss.hasPermission('product:category:query')")
    public CommonResult<PageResult<CategoryRespVO>> getCategoryPage(@Valid CategoryPageReqVO pageVO) {
        PageResult<CategoryDO> pageResult = categoryService.getCategoryPage(pageVO);
        return success(CategoryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出分类信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:category:export')")
    @OperateLog(type = EXPORT)
    public void exportCategoryExcel(@Valid CategoryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CategoryDO> list = categoryService.getCategoryList(exportReqVO);
        // 导出 Excel
        List<CategoryExcelVO> datas = CategoryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "分类信息.xls", "数据", CategoryExcelVO.class, datas);
    }

}
